﻿using System.Linq;
using Enyim.Caching;
using Web.OnlinePay;
using System.Web.Mvc;
using SilenthinkContract;
using SilenthinkContract.Domain;
using SilenthinkCommon.Extension;
using SilenthinkContract.Component;
using Web.VirtualProductHandler;
using System.Collections.Specialized;

namespace Web.Areas.Pay.Controllers
{
    public class NotifyController : PayBaseController
    {

        public NotifyController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public string Index(string param)
        {
            return string.Empty;
        }

        public void ChinaPay()
        {
            var pay = GetOnlinePay(1);
            var result = pay.NotityHandler(Request.QueryString);
            if (result.Status)
            {
                var data = result.Data;
                var extra = result.Extra as NameValueCollection;
                var payTrade = PaymentService.GetPayTrade(data.No);
                if (payTrade != null)
                {
                    if (payTrade.Status == 0)
                    {
                        return;
                    }
                    if (payTrade.Version == data.Version && payTrade.Amount == data.Amount)
                    {
                        var status = PaymentService.UpdatePayTrade(payTrade);
                        if (status)
                        {
                            InternalHandler(payTrade);
                        }
                        else
                        {
                            //写入日志
                        }
                    }
                    else
                    {
                        //版本号或金额不一致
                    }
                }
                else
                {
                    //交易号不存在
                }
            }
            else
            {
                var message = result.Message;
                //写入日志
            }
        }

        public string TenPay()
        {
            var s = string.Empty;
            var pay = GetOnlinePay(2);
            var result = pay.NotityHandler(Request.QueryString);
            if (result.Status)
            {
                var data = result.Data;
                var trade = PaymentService.GetPayTrade(data.No);
                if (trade != null)
                {
                    if (trade.Status == 0)
                    {
                        s = "success";
                    }
                    else
                    {
                        var status = PaymentService.UpdatePayTrade(trade);
                        if (!status)
                        {
                            s = "fail";
                            //写入日志
                        }
                        else
                        {
                            s = "success";
                        }
                    }
                }
            }
            else
            {
                s = "fail";
                var message = result.Message;
                //写入日志
            }
            return s;
        }
    }
}
